package manager
{
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import utils.DisplayUtils;
	import utils.StageReferance;
	
	public class SpriteLayer extends Sprite
	{
		public function SpriteLayer(enableMouse:Boolean = false)
		{
			init();
			super();
			mouseEnabled = enableMouse;
		}
		
		private var _alphaBackgoundFilter:Array = []
		private function init():void
		{
			_blackGoundList = new Vector.<DisplayObject>();
			_alphaGoundList = new Vector.<DisplayObject>();
		}
		
		private var _blackGoundList:Vector.<DisplayObject>;
		private var _alphaGoundList:Vector.<DisplayObject>;
		private var _blackGound:Sprite;
		public function addTolayer(child:DisplayObject,blockGound:int,focusTop:Boolean):void
		{
			if(blockGound == LayerManager.BLCAK_BLOCKGOUND)
			{
				if(_blackGoundList.indexOf(child) != -1)
				{
					_blackGoundList.splice(_blackGoundList.indexOf(child),1);
				}
				_blackGoundList.push(child);
			}else if(blockGound == LayerManager.ALPHA_BLOCKGOUND)
			{
				if(_alphaGoundList.indexOf(child) != -1)
				{
					_alphaGoundList.splice(_alphaGoundList.indexOf(child),1);
				}
				_alphaGoundList.push(child);
			}
			child.addEventListener(Event.REMOVED_FROM_STAGE,__onChildRemoveFromStage);
			if(focusTop)child.addEventListener(Event.REMOVED_FROM_STAGE,__onFocusChange);
			if(_autoClickTotop)child.addEventListener(MouseEvent.MOUSE_DOWN,__onClickToTop);
			addChild(child);
			arrangeBlockGound();
			if(focusTop)focusTopLayerDisplay();
		}

		private function __onClickToTop(event:MouseEvent):void
		{
			var child:DisplayObject = event.currentTarget as DisplayObject;
			addChild(child);
			focusTopLayerDisplay();
		}
		
		
		private function __onFocusChange(event:Event):void
		{
			var child:DisplayObject = event.currentTarget as DisplayObject;
			child.removeEventListener(Event.REMOVED_FROM_STAGE,__onFocusChange);
			focusTopLayerDisplay(child);
		}

		private function __onChildRemoveFromStage(event:Event):void
		{
			var child:DisplayObject = event.currentTarget as DisplayObject;
			child.removeEventListener(Event.REMOVED_FROM_STAGE,__onChildRemoveFromStage);
			child.removeEventListener(MouseEvent.MOUSE_DOWN,__onClickToTop);
			if(_blackGoundList.indexOf(child)!=-1)_blackGoundList.splice(_blackGoundList.indexOf(child),1);
			if(_alphaGoundList.indexOf(child)!=-1)_alphaGoundList.splice(_alphaGoundList.indexOf(child),1);
			arrangeBlockGound();
		}
		
		private function arrangeBlockGound():void
		{
			if(blackGound.parent)blackGound.parent.removeChild(blackGound);
			if(alphaGound.parent)alphaGound.parent.removeChild(alphaGound);
			var child:DisplayObject;
			var childIndex:int;
			if(_blackGoundList.length > 0)
			{
				child = _blackGoundList[_blackGoundList.length-1];
				childIndex = getChildIndex(child);
				addChildAt(blackGound,childIndex);
			}
			
			if(_alphaGoundList.length > 0)
			{
				child = _alphaGoundList[_alphaGoundList.length-1];
				childIndex = getChildIndex(child);
				addChildAt(alphaGound,childIndex);
			}
		}
		
		private function focusTopLayerDisplay(exclude:DisplayObject = null):void
		{
			var lastFocus:InteractiveObject;
			for(var i:int = 0;i<numChildren;i++)
			{
				var child:DisplayObject = getChildAt(i);
				if(child == exclude)continue;
				lastFocus = child as InteractiveObject;
			}
			if(!DisplayUtils.isTargetOrContain(StageReferance.stage.focus,lastFocus))
			{
				StageReferance.stage.focus = lastFocus;
			}
		}
		/**
		 * 获得是否有蒙版在舞台上
		 */		
		public function get backGroundInParent():Boolean
		{
			if(_blackGoundList.length > 0 || _alphaGoundList.length >0)
			{
				return true;
			}else
			{
				return false;
			}
		}
		
		private function get blackGound():Sprite
		{
			if(_blackGound == null)
			{
				_blackGound = new Sprite();
				_blackGound.graphics.beginFill(0x000000,0.4);
				_blackGound.graphics.drawRect(-500,-200,2000,1000);
				_blackGound.graphics.endFill();
				_blackGound.addEventListener(MouseEvent.MOUSE_DOWN,__onBlackGoundMouseDown);
			}
			return _blackGound;
		}

		private function __onBlackGoundMouseDown(event:MouseEvent):void
		{
			event.stopImmediatePropagation();
			StageReferance.stage.focus = _blackGoundList[_blackGoundList.length-1] as InteractiveObject;
		}
		
		private var _alphaGound:Sprite;
		private function get alphaGound():Sprite
		{
			if(_alphaGound == null)
			{
				_alphaGound = new Sprite();
				_alphaGound.graphics.beginFill(0x000000,0.001);
				_alphaGound.graphics.drawRect(-500,-200,2000,1000);
				_alphaGound.graphics.endFill();
				_alphaGound.addEventListener(MouseEvent.MOUSE_DOWN,__onAlphaGoundDownClicked);
				_alphaGound.addEventListener(MouseEvent.MOUSE_UP,__onAlphaGoundUpClicked);
			}
			return _alphaGound;
		}

		private function __onAlphaGoundDownClicked(event:MouseEvent):void
		{
			var target:DisplayObject = _alphaGoundList[_alphaGoundList.length - 1];
			target.filters = _alphaBackgoundFilter;
			StageReferance.stage.focus = target as InteractiveObject;
		}
		
		private function __onAlphaGoundUpClicked(event:MouseEvent):void
		{
			var target:DisplayObject = _alphaGoundList[_alphaGoundList.length - 1];
			target.filters = null;
		}
		
		private var _autoClickTotop:Boolean;
		public function set autoClickTotop(value:Boolean):void
		{
			if(_autoClickTotop == value)return;
			_autoClickTotop = value;
		}
	}
}